package org.anddev.andengine.extension.svg.node;
/**
 * @author Eric Depta
 * @since 01:14:00 - 28.02.2011
 */
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.extension.svg.SVGDoc;
import org.anddev.andengine.extension.svg.SVGStyle;
import org.anddev.andengine.extension.svg.util.parser.ParserUtil;
import org.anddev.andengine.extension.svg.util.transform.TransformDoc;
import org.anddev.andengine.util.SAXUtils;
import org.xml.sax.Attributes;

import com.badlogic.gdx.physics.box2d.Body;

public abstract class DrawablePhysicNode extends TransformableNode {
	protected boolean hasShape = false;
	protected boolean hasPhysic = false;
	protected boolean drawPrimitives = false;
	protected boolean hasSprite = false;
	
	public DrawablePhysicNode(final SVGDoc pSVGDoc){
		super(pSVGDoc);
	}
	
	public void create(final Attributes pAttributes, final TransformDoc pTransform){
		super.create(pAttributes, pTransform);
		this.setHasPhysic(SAXUtils.getBooleanAttribute(pAttributes, TAG_ATTRIBUTE_HASPHYSIC, false));
		this.setHasShape(SAXUtils.getBooleanAttribute(pAttributes, TAG_ATTRIBUTE_HASSHAPE, false));
		this.setDrawPrimitives(SAXUtils.getBooleanAttribute(pAttributes, TAG_ATTRIBUTE_DRAWPRIMITIVE, false));
		
		if(this.isHasPhysic() || this.isHasShape() || this.isDrawPrimitives()){
			final String name = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ATTRIBUTE_ID);
			Shape shape = this.createShape(pAttributes);
			Body body = this.createPhysics(shape, pAttributes);
			//TODO if valid
			if(!this.isHasDrawables()){
				shape = null;
			}
			if(body!=null || shape!=null){
				super.mSVGDoc.addElement(name, shape, body);
			}
			
		}
	}
	
	protected void drawPrimitives(final Attributes pAttributes, final Shape pShape){
		if(this.isDrawPrimitives() && !super.mSVGDoc.getDebug()){
			final SVGStyle style = new SVGStyle(pAttributes.getValue(TAG_ATTRIBUTE_STYLE));
			//- Fill
			String colorstr = style.get(TAG_ATTRIBUTE_STYLE_FILLCOLOR);
			if(!colorstr.equals("none")){
				this.drawPrimitivesFill(pShape, ParserUtil.getColor(colorstr), style.getFloatValue(TAG_ATTRIBUTE_STYLE_FILLALPHA, 1));
			}
			//- Line
			colorstr = style.get(TAG_ATTRIBUTE_STYLE_LINECOLOR);
			if(!colorstr.equals("none")){
				this.drawPrimitivesLine(pShape, ParserUtil.getColor(colorstr), style.getFloatValue(TAG_ATTRIBUTE_STYLE_LINEALPHA, 1), style.getFloatValuePx(TAG_ATTRIBUTE_STYLE_LINEWIDTH, 1), super.mSVGDoc.getLineAntiAlias());
			}
			
		}else if(super.mSVGDoc.getDebug()){
			if(super.mSVGDoc.getDebugFill()){
				this.drawPrimitivesFill(pShape, super.mSVGDoc.getDebugFillColor(), super.mSVGDoc.getDebugFillAlpha());
			}
			if(super.mSVGDoc.getDebugLine()){
				this.drawPrimitivesLine(pShape, super.mSVGDoc.getDebugLineColor(), super.mSVGDoc.getDebugLineAlpha(), super.mSVGDoc.getDebugLineWidth(), super.mSVGDoc.getLineAntiAlias());
			}
		}
	}
	
	protected abstract Shape createShape(final Attributes pAttributes);
	protected abstract Body createPhysics(final Shape pShape, final Attributes pAttributes);
	protected abstract void drawPrimitivesFill(final Shape pShape, final float[] pColor, final float pAlpha);
	protected abstract void drawPrimitivesLine(final Shape pShape, final float[] pColor, final float pAlpha, final float pWidth, final boolean pAntiAlias);
	
	public boolean isHasShape() {
		return this.hasShape;
	}

	public void setHasShape(boolean pHasShape) {
		this.hasShape = pHasShape;
	}

	public boolean isHasPhysic() {
		return this.hasPhysic;
	}

	public void setHasPhysic(boolean pHasPhysic) {
		this.hasPhysic = pHasPhysic;
	}

	public boolean isDrawPrimitives() {
		return this.drawPrimitives;
	}

	public void setDrawPrimitives(boolean pDrawPrimitives) {
		this.drawPrimitives = pDrawPrimitives;
	}

	public boolean isHasSprite() {
		return hasSprite;
	}

	public void setHasSprite(boolean pHasSprite) {
		this.hasSprite = pHasSprite;
	}
	
	public boolean isHasDrawables(){
		return this.isHasSprite() || this.isDrawPrimitives() || super.mSVGDoc.getDebug();
	}
}
